package com.manage.utils;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Fitch
 */
public class ImageCombineTest {

    public static void main(String[] args) {


       test2();

    }
    private static void test1(){
        long t1= Calendar.getInstance().getTimeInMillis();
        //读取文件
        try {
            BufferedImage bi_0 = ImageIO.read(new File("C:\\KDR\\HE.png"));
            BufferedImage bi_1 = ImageIO.read(new File("C:\\KDR\\HR.png"));
            //假设图片0 和图片1 宽度相同，上下合成
            //new 一个新的图像
            int w = bi_0.getWidth();
            int h_0 = 200;
            int h_1 = 100;
            int h = h_0 + h_1;
            BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);

            //像素一个一个复制过来
            for (int y = 0; y < h_0; y++) {
                for (int x = 0; x < w; x++) {
                    bi.setRGB(x, y, bi_0.getRGB(x, y));
                }
            }
            for (int y = 0; y < h_1; y++) {
                for (int x = 0; x < w; x++) {
                    bi.setRGB(x, h_0 + y, bi_1.getRGB(x, y));
                }
            }
            ImageIO.write(bi, "JPEG", new File("C:\\KDR\\HT.png"));
            long t2= Calendar.getInstance().getTimeInMillis();
            System.out.println(t2-t1);
        } catch (IOException ex) {
            System.out.println("异常："+ex);
        }
    }
    private static void test2(){
        try {
            int i = 1;
            int j = 2;
            InputStream imagein = new FileInputStream("C:\\KDR\\HE.png");
            InputStream imagein2 = new FileInputStream("C:\\KDR\\HR.png");

            BufferedImage image = ImageIO.read(imagein);
            BufferedImage image2 = ImageIO.read(imagein2);
            Graphics g = image.getGraphics();
            g.drawImage(image2, image.getWidth() - image2.getWidth() - 15, image.getHeight() - image2.getHeight() - 10,
                    image2.getWidth() + 10, image2.getHeight() + 5, null);
            OutputStream outImage = new FileOutputStream("C:\\KDR\\HY.png");
            JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(outImage);
            enc.encode(image);
            imagein.close();
            imagein2.close();
            outImage.close();
        }catch (Exception e){
            System.out.println(e);
        }

    }

}
